package com.bnz.servlet;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.Student;
import com.bnz.pojo.StudentVo;
import com.bnz.service.StudentService;
import com.bnz.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {

    private StudentService studentService = new StudentServiceImpl();

    @Override
    public void init() throws ServletException {

        studentService = new StudentServiceImpl();

    }

    /**
     * 1. 根据不同的请求参数调用不同的处理方法
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.0 处理post请求的中文码
        req.setCharacterEncoding("utf-8");
        //1.1 设置响应对象的编码类型
        resp.setContentType("text/html;charset=utf8");
        //1.2 得到请求参数
        String cmd = req.getParameter("cmd");
        //1.3 根据请求参数的值来调用不同的方法
        if (StrUtil.isNotBlank(cmd)){       //1.列表学生
            if ("list".equals(cmd)){
                list(req,resp);
            }
            if ("findByPage".equals(cmd)){  //2. 分页查询
                findByPage(req,resp);
            }
            if ("search".equals(cmd)){      //3. 条件查询带分页
                search(req,resp);
            }
        }
    }




    /**
     * 2. 学生列表
     * @param req
     * @param resp
     */
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //2.1 查询所有学生
        List<Student> students = studentService.findAll();
        //2.2 转换为json串
        String s = JSON.toJSONString(students);
        //2.3 输出到页面
        resp.getWriter().println(s);
        //2.4 刷新页面
        resp.getWriter().flush();
    }

    /**
     * 3. 分页查询
     * @param req
     * @param resp
     */
    private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //3.1 得到分页查询的参数
        int page = new Integer(req.getParameter("page"));           //当前页
        int pagesize = new Integer(req.getParameter("pagesize"));   //每页大小
        //3.2 开始分页
        PageResult<Student> pr = studentService.findByPage(page,pagesize);
        //3.3 转换为json串
        String s = JSON.toJSONString(pr);
        //3.4 输出到页面
        resp.getWriter().println(s);
        //3.5 刷新页面
        resp.getWriter().flush();
    }

    /**
     * 4. 条件查询带分页
     * @param req
     * @param resp
     * @throws IOException
     */
    private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //4.1 得到分页查询的参数
        int page = new Integer(req.getParameter("page"));               //当前页
        int pagesize = new Integer(req.getParameter("pagesize"));       //每页大小
        //4.2 得到查询参数
        String stud = req.getParameter("stud");
        //4.3 转换为java对象studentVo
        StudentVo vo = JSON.parseObject(stud,StudentVo.class);
        System.out.println("vo = " + vo);
        //4.4 开始条件查询带分页
        PageResult<Student> pr = studentService.search(page,pagesize,vo);
        //4.5 转换为json对象
        String s = JSON.toJSONString(pr);
        //4.6 输出页面
        resp.getWriter().println(s);
        //4.7 刷新页面
        resp.getWriter().flush();

    }
}
